using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using OpenComposite.Base.Designer;
using OpenComposite.EII.Designer.BRules;
using OpenComposite.EII.Designer.Generic;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.Designer.BusinessProcess
{
	public class WhileSymbol : Symbol, IConnector, IConnectorFactory
	{
		#region Initialization
		public WhileSymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			//this.Name = "whileSymbol" + GetCount(typeof(WhileSymbol));
			this.Text = "While";

			this.Border = BorderType.Rectangle;
			this.BorderColor = Color.Blue;
			this.BorderWidth = 3;

			this.Size = new Size(200, 200);
			this.MinimumSize = new Size(100, 100);

			//
			// _start
			//
			_start = new StartSymbol();
			_start.Location = new Point(5, (int)( 100 - _start.Height / 2f ));
			_start.Anchor = System.Windows.Forms.AnchorStyles.Left;

			//
			// _end
			//
			_end = new EndSymbol();
			_end.Location = new Point(this.ClientRectangle.Width - _end.Width - 5, (int)( 100 - _end.Height / 2f ));
			_end.Anchor = System.Windows.Forms.AnchorStyles.Right;

			this.Controls.Add(_start);
			this.Controls.Add(_end);

			this.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Override

		#region Properties
		public override bool CanFocused
		{
			get
			{
				return true;
			}
		}
		public override bool CanMove
		{
			get
			{
				return true;
			}
		}
		public override bool CanResize
		{
			get
			{
				return true;
			}
		}
		public override bool CanSelect
		{
			get
			{
				return true;
			}
		}
		public override bool IsContainer
		{
			get
			{
				return true;
			}
		}
		public override bool NoClip
		{
			get
			{
				return false;
			}
		}
		#endregion

		#region Drag & Drop
		public override object GetDragObject(Point location, System.Windows.Forms.MouseButtons button)
		{
			return this;
		}
		public override bool IsDropAllowed(Type type, Point location, System.Windows.Forms.MouseButtons button)
		{
			return ( ( type == typeof(IFSymbol) || type == typeof(ANDSymbol) || type == typeof(ORSymbol) ||
					   type == typeof(BusinessProcess.ActivitySymbol) || type == typeof(WhileSymbol) ) );
		}
		public override bool IsDropAllowed(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			return ( this.IsDropAllowed(obj.GetType(), location, button) && obj != this );
		}
		public override void DropObject(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			RepositoryService repSvc = (RepositoryService)this.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			if ( this.IsDropAllowed(obj, location, button) ) {
				if ( button == System.Windows.Forms.MouseButtons.Right ) {
					this.DesignerViewControl.AddRoute((DesignerControl)obj, this);
				} else if ( button == System.Windows.Forms.MouseButtons.Left ) {
					DesignerControl dc = null;
					if ( obj is RepositoryItemBase ) {
						dc = repSvc.CreateDesignerControl((RepositoryItemBase)obj);
					} else {
						dc = obj as DesignerControl;
					}
					if ( dc != null ) {
						dc.Location = this.PointToDesignerControlClient(location);
						this.Controls.Add(dc);
					}
				}
			}
		}
		#endregion

		#region Paint
		public override void PaintBackground(System.Windows.Forms.PaintEventArgs pe)
		{
			base.PaintBackground(pe);
		}
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			base.Paint(pe);
		}
		#endregion

		#endregion

		#region Properties
		public StartSymbol StartSymbol
		{
			get { return _start; }
		}
		public EndSymbol EndSymbol
		{
			get { return _end; }
		}
		#endregion

		#endregion

		#region Protected Members

		#region Override
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private StartSymbol _start;
		private EndSymbol _end;
		#endregion

		#endregion

		#region IConnectorFactory Members

		public override bool CreateRouteEnd(DesignerControl controlStart,
			out IConnector connector, out System.Drawing.Drawing2D.LineCap cap,
			out System.Drawing.Drawing2D.CustomLineCap customcap, out RouteDockDirection dockStart,
			out RouteDockDirection dockEnd, out bool useStartConnectorAsRouteStart)
		{
			return base.CreateRouteEnd(controlStart,
				out connector, out cap, out customcap, out dockStart, out dockEnd, out useStartConnectorAsRouteStart);
		}

		public override bool CreateRouteStart(DesignerControl controlEnd,
			out IConnector connector, out System.Drawing.Drawing2D.LineCap cap,
			out System.Drawing.Drawing2D.CustomLineCap customcap, out RouteDockDirection dockStart,
			out RouteDockDirection dockEnd, out bool useEndConnectorAsRouteEnd)
		{
			return base.CreateRouteStart(controlEnd,
				out connector, out cap, out customcap, out dockStart, out dockEnd, out useEndConnectorAsRouteEnd);
		}

		#endregion
	}
}
